package com.jse.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpCookie;
import java.util.Map;

import com.jse.Lang;
import com.jse.json.JsonObject;

public interface Ctx extends Map<String,Object> {

	default void parse(Object req) {};
	default void parse(Object req,Object res) {};
	default boolean isForward(){return false;};
	void forward(String url);
	
	Object socket();
	Object req();
	Object res();

	JsonObject tbl();
	String body();
	String getMethod();

	String getHeader(String name);

	void setAttribute(String name, Object value);

	Object getAttribute(String name);

	Object getSession();

	String getRequestedSessionId();

	Map<String, HttpCookie> cookies();

	String reqcontentType();

	String getContentType();

	void setContentType(String type);

	void setStatus(int status);

	void sendError(int status, String msg);

	OutputStream getResponseBody();

	boolean isCommitted();
	
	void sattr(String name, Object value);

	Object sattr(String name);
	void attr(String name, Object value);

	Object attr(String name);
	
	void app(String name, Object value);

	Object app(String name);

	Map<String, Object> sessionMap();

	void setHeader(String name, String value);

	InputStream getRequestBody();

	Map<String, Object> attr();

	int httpStatus();

	void sendResponseHeaders(int code, long len) throws IOException;

	String getRemoteAddr();

	String getServletPath();
	/** 转义过的servletPath */
	String getPath();
	
	String getSuffix();

	String getMapping();

	String getQueryString();

	String getServerName();

	long time();
	default boolean isMobile() {return Lang.isMobile(getHeader("user-agent"));}
	default boolean isAndroid() {return Lang.isAndroid(getHeader("user-agent"));}
	default boolean isIos() {return Lang.isIOS(getHeader("user-agent"));}
	default boolean isWeChat(){return Lang.isWeChat(getHeader("user-agent"));}
	default boolean isAjax() {
	 String xRequestedWith = getHeader("x-requested-with");
	 if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)return true;
	 return isJson();
	}
	default boolean isJson() {
	 String accept = getHeader("accept");
	 if (accept != null && accept.indexOf("application/json") != -1)return true;
	 String type = getHeader("content-type");
	 if (type != null && type.indexOf("application/json") != -1)return true;
	 return getSuffix().equals("json");
	}
	void clear();
	void invalidate();
	void sattrdel(String name);
	void redirect(String url);
}
